<?php
namespace SmartDevice\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\View\Model\JsonModel;
// use Album\Model\Album; // <-- Add this import
// use Album\Form\AlbumForm; // <-- Add this import
use SmartDevice\Model\Owner;
use Zend\Http\PhpEnvironment\Request;
use Zend\Http\Headers;

class SmartDeviceController extends AbstractActionController
{

	protected $ownerTable;
	
	public function justTestAction() {
		echo ("This is just a test!");
		exit();
	}
	
	public function testOwnerAction () {
		$owner = new Owner();
		$owner->ownerId = 0;
    	$owner->email = "kang.sun@gmail.com";
    	$owner->phoneNo = "186-2153-3750";
    	$owner->authToken = "01234567890123456789012345678901";
    	$owner->createdAt = 1234567;
    	$owner->updatedAt = 7654342;
    	$hello= $this->getOwnerTable();
    	$this->getOwnerTable()->saveOwner($owner);
		
		$response = $this->getResponse();
		$response->setStatusCode(404);
		$variables = array( 'Foo' => 'Bar', 'Baz' => 'Test', "ServerEnv" => getenv("server-env"));

		$json = new JsonModel( $variables );
		$response->setContent("{'hello':'world}");

		$hello = hash('sha1', 'test');
		
		echo($hello."\n\n");
				
		return $json;
	}

	public function registownerAction() {
		echo ("PIG");
		exit();
	}
// 	public function jsonRpcApiAction () {
// 		/*
// 		 print_r($this->getRequest()->getRequestUri()."<br/><br/>");
// 		print_r("<br/><br/>");
// 		print_r("\n");

// 		print_r($this->getRequest()->getServer('REQUEST_METHOD')."<br/><br/>");
// 		print_r("<br/><br/>");
// 		print_r("\n");

// 		$headers = $this->getRequest()->getHeaders();
// 		print_r("<br/><br/>");
// 		print_r("\n");
// 		print_r($headers->get("User-Agent")->getFieldValue());
// 		print_r("<br/><br/>");
// 		print_r("\n");
// 		print ($this->getRequest()->getQuery("someid"));
// 		print_r("<br/><br/>");
// 		print_r("\n");
// 		print ($this->getRequest()->getContent());
// 		print_r("\n");
// 		*/

		 
// 		//         exit();
// 		//         $json = new JsonModel($this->request);


// 		$response = $this->getResponse();
// 		$response->setStatusCode(404);
// 		$variables = array( 'Foo' => 'Bar', 'Baz' => 'Test', "ServerEnv" => getenv("server-env"));

// 		$json = new JsonModel( $variables );
// 		//         $response->setContent("{'hello':'world}");
// 		//     print_r("HELLO");

// 		return $json;

// 	}

// 	public function jsonAction() {
// 		$variables = array( 'Foo' => 'Bar', 'Baz' => 'Test' );
// 		$json = new JsonModel( $variables );

// 		return $json;
// 	}



// 	public function indexAction()
// 	{
// 		return new ViewModel(array(
// 				'albums' => $this->getAlbumTable()->fetchAll()
// 		));
// 	}

// 	// Add content to this method:
// 	public function addAction()
// 	{
// 		$form = new AlbumForm();
// 		$form->get('submit')->setValue('Add');

// 		$request = $this->getRequest();
// 		if ($request->isPost()) {
// 			$album = new Album();
// 			$form->setInputFilter($album->getInputFilter());
// 			$form->setData($request->getPost());

// 			if ($form->isValid()) {
// 				$album->exchangeArray($form->getData());
// 				$this->getAlbumTable()->saveAlbum($album);

// 				// Redirect to list of albums
// 				return $this->redirect()->toRoute('album');
// 			}
// 		}
// 		return new ViewModel(array(
// 				'form' => $form
// 		));
// 	}

// 	public function editAction()
// 	{
// 		$id = (int) $this->params()->fromRoute('id', 0);
// 		if (!$id) {
// 			return $this->redirect()->toRoute('album', array(
// 					'action' => 'add'
// 			));
// 		}
// 		$album = $this->getAlbumTable()->getAlbum($id);
		 
// 		$form = new AlbumForm();
// 		$form->bind($album);
// 		$form->get('submit')->setAttribute('value', 'Edit');
		 
// 		$request = $this->getRequest();
// 		if($request->isPost()) {
// 			$form->setInputFilter($album->getInputFilter());
// 			$form->setData($request->getPost());
				
// 			if ($form->isValid()) {
// 				$this->getAlbumTable()->saveAlbum($form->getData());
// 				return $this->redirect()->toRoute('album');
// 			}
// 		}
		 
// 		return array(
// 				'id' => $id,
// 				'form' => $form,
// 		);
// 	}

// 	public function deleteAction()
// 	{
// 		$id = (int) $this->params()->fromRoute('id', 0);
// 		if (!$id) {
// 			return $this->redirect()->toRoute('album');
// 		}
		 
// 		$request = $this->getRequest();
// 		if ($request->isPost()) {
// 			$del = $request->getPost('del', 'NO');

// 			if ($del == 'Yes') {
// 				$id = (int) $request->getPost('id');
// 				$this->getAlbumTable()->deleteAlbum($id);
// 			}
				
// 			return $this->redirect()->toRoute('album');
// 		}
		 
// 		return array(
// 				'id' => $id,
// 				'album' => $this->getAlbumTable()->getAlbum($id)
// 		);
// 	}

	public function getOwnerTable()
	{
		if (! $this->ownerTable) {
			$sm = $this->getServiceLocator();
			$this->ownerTable = $sm->get('SmartDevice\Model\OwnerTable');
		}
		return $this->ownerTable;
	}
}